package com.zhuoya.healthcard;

import com.zhuoya.healthcard.entity.*;

import android.app.AlertDialog;
import android.widget.*;

import com.ldoublem.loadingviewlib.view.*;
import com.zhuoya.healthcard.contants.*;
import com.epson.isv.eprinterdriver.Ctrl.*;

import android.view.*;

import com.zkteco.android.IDReader.*;
import com.alibaba.fastjson.*;

import android.graphics.*;

import com.zhuoya.healthcard.util.*;

import android.content.*;

import android.os.*;

public class PersonInfoActivity extends BaseActity {
    private OrderInfo info;
    private TextView mAge;
    private TextView mConfirm;
    private TextView mIdcard;
    private LVCircularZoom mLoading;
    private TextView mName;
    private TextView mNation;
    private TextView mPhone;
    private TextView mSxe;
    private TextView mValidityTimeEnd;
    private TextView mValidityTimeStart;
    private TextView mWork;
    private int pageStatus;
    private String sex;

    private void initView() {
        this.mName = this.findViewById(R.id.textView22);
        this.mAge = this.findViewById(R.id.textView23);
        this.mValidityTimeStart = this.findViewById(R.id.textView24);
        this.mPhone = this.findViewById(R.id.textView25);
        this.mWork = this.findViewById(R.id.textView26);
        this.mSxe = this.findViewById(R.id.textView32);
        this.mIdcard = this.findViewById(R.id.textView33);
        this.mValidityTimeEnd = this.findViewById(R.id.textView34);
        this.mNation = this.findViewById(R.id.textView35);
        this.mConfirm = this.findViewById(R.id.textView12);
        (this.mLoading = this.findViewById(R.id.loading)).setVisibility(View.INVISIBLE);
        final OrderInfo info = this.info;
        if (info != null) {
            this.mName.setText((CharSequence) SensitiveInfoUtils.chineseName(info.getName()));
            final TextView mAge = this.mAge;
            final StringBuilder sb = new StringBuilder();
            sb.append(this.info.getAge());
            sb.append("");
            mAge.setText((CharSequence) sb.toString());
            this.mPhone.setText((CharSequence) SensitiveInfoUtils.mobilePhone(this.info.getPhone()));
            this.mWork.setText((CharSequence) this.info.getWork());
            String sex;
            if (this.info.getSex() == 1) {
                sex = "男";
            } else {
                sex = "女";
            }
            this.sex = sex;
            this.mSxe.setText((CharSequence) this.sex);
            this.mIdcard.setText((CharSequence) SensitiveInfoUtils.idCard(this.info.getIdcard()));
            this.mNation.setText((CharSequence) this.info.getNation());
            final String[] split = this.info.getValidityTime().split("-");
            this.mValidityTimeStart.setText((CharSequence) split[0]);
            this.mValidityTimeEnd.setText((CharSequence) split[1]);
        }
        // 爱普生打印参数配置
        final EPSetting instance = EPSetting.instance();
        instance.setSelEpsPrinter(Contants.epsPrinter);
        instance.setSelPageAttri(
                new PageAttribute(
                        new Integer(Contants.PARER_TYPE),
                        new Integer(PageAttribute.MediaTypeID.EPS_MTID_PLAIN),
                        new Integer(PageAttribute.PrintQuality.EPS_MQID_NORMAL)));
        instance.setPrintDirection(new Integer(0));
        instance.setColorMode(new Integer(0));
        instance.setPaperSource(new Integer(128));
        instance.setBorderless(new Boolean(false));
        instance.setDuplexPrint(new Boolean(false));
        instance.setTotalPages(1);


        final StringBuilder sb2 = new StringBuilder();
        sb2.append(this.getCacheDir().getAbsolutePath());
        sb2.append("/temp.jpg");
        instance.setTemporaryImageFilePath(sb2.toString());
        this.mConfirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(final View view) {
                if (PersonInfoActivity.this.mLoading.getVisibility() == View.INVISIBLE) {
                    PersonInfoActivity.this.mLoading.setVisibility(View.VISIBLE);
                    PersonInfoActivity.this.mLoading.startAnim();
                    new Thread(new Runnable() {
                        @Override
                        public void run() {
                            try {
                                final byte[] array = new byte[WLTService.imgLength];
                                if (1 == WLTService.wlt2Bmp(PersonInfoActivity.this.info.getPhoto(), array)) {
                                    final Bitmap bgr2Bitmap = IDPhotoHelper.Bgr2Bitmap(array);
                                    final String bitmapToBase64 = BitmapUtil.bitmapToBase64(bgr2Bitmap);
                                    final JSONObject param = new JSONObject();
                                    param.put("orderId", PersonInfoActivity.this.info.getId());
                                    param.put("base64", bitmapToBase64);
                                    param.put("name", PersonInfoActivity.this.info.getName());
                                    param.put("national", PersonInfoActivity.this.info.getNation());
                                    param.put("age", PersonInfoActivity.this.info.getAge());
                                    param.put("gender", PersonInfoActivity.this.info.getSex());
                                    param.put("idCard", PersonInfoActivity.this.info.getIdcard());
                                    param.put("hospitalId", Contants.HOSPITALID);
                                    param.put("districtId", Contants.DISTRICTID);
                                    param.put("cityId", Contants.CITYID);
                                    param.put("provinceId", Contants.PROVINCEID);
                                    // 保存登记信息
                                    final String result = HttpUtil.submitPostData(Contants.uri + "order/saveRegistration", param, "utf_8");
                                    final JSONObject resJSON = JSON.parseObject(result);
                                    final int code  = resJSON.getInteger("code");
                                    final String msg = resJSON.getString("msg");
                                    if (code == 0) {
                                        PersonInfoActivity.this.runOnUiThread((Runnable) new Runnable() {
                                            @Override
                                            public void run() {
                                                String string = null;
                                                try {
                                                    String number = resJSON.getString("number");
                                                    String registrationId = resJSON.getString("registrationId");
                                                    //String sshy = resJSON.getString("sshy");
                                                    if (Contants.PARER_TYPE == 0) { // A4打印
                                                        ImageUtil.saveBitmap(ImageUtil.addTextA4(bgr2Bitmap,
                                                                number,
                                                                PersonInfoActivity.this.info.getName(),
                                                                PersonInfoActivity.this.info.getIdcard(),
                                                                PersonInfoActivity.this.info.getSex() == 1 ? "男" : "女",
                                                                PersonInfoActivity.this.info.getAge() + "",
                                                                PersonInfoActivity.this.info.getNation(),
                                                                PersonInfoActivity.this.info.getWork()), "/mnt/internal_sd/Download/", "tempRegistration.jpg");
                                                    } else { //A5打印
                                                        ImageUtil.saveBitmap(ImageUtil.addTextA5(
                                                                PersonInfoActivity.this.info.getName(),
                                                                PersonInfoActivity.this.info.getSex() == 1 ? "男" : "女",
                                                                PersonInfoActivity.this.info.getAge() + "",
                                                                number,
                                                                PersonInfoActivity.this.info.getWorkName(),
                                                                PersonInfoActivity.this.info.getIsExpress()), "/mnt/internal_sd/Download/", "tempRegistration.jpg");
                                                    }

                                                    if(EPrintManager.instance().isPrintBusy()) {
                                                        new AlertDialog.Builder(PersonInfoActivity.this)
                                                                .setTitle("温馨提示")
                                                                .setMessage("打印机忙碌中，请稍后重试")
                                                                .setPositiveButton("确定", new DialogInterface.OnClickListener(){
                                                                    @Override
                                                                    public void onClick(DialogInterface dialog, int which) {
                                                                        dialog.dismiss();
                                                                    }
                                                                })
                                                                .create().show();
                                                        return;
                                                    }
                                                    Intent intent = new Intent();
                                                    intent.setClass( PersonInfoActivity.this, PrintActivity.class);
                                                    intent.putExtra("imgUrl", "/mnt/internal_sd/Download/tempRegistration.jpg");
                                                    intent.putExtra("opCode", 1);
                                                    intent.putExtra("registrationId", registrationId);
                                                    startActivity(intent);

                                                } catch (Exception ex) {
                                                    ex.printStackTrace();
                                                    // 异常回调，删除登记信息
                                                    new Thread(new Runnable() {
                                                        @Override
                                                        public void run() {
                                                            final JSONObject jsonObject = new JSONObject();
                                                            jsonObject.put("registrationId", resJSON.getString("registrationId"));
                                                            HttpUtil.submitPostData(Contants.uri + "order/registrationRecall", jsonObject, "utf-8");
                                                        }
                                                    }).start();
                                                    PersonInfoActivity.this.runOnUiThread((Runnable) new Runnable() {
                                                        @Override
                                                        public void run() {
                                                            PersonInfoActivity.this.mLoading.setVisibility(View.INVISIBLE);
                                                            final Intent intent = new Intent((Context) PersonInfoActivity.this,  ErrorActivity.class);
                                                            intent.putExtra("code", 1);
                                                            intent.putExtra("pageStatus", PersonInfoActivity.this.pageStatus);
                                                            intent.putExtra("errorInfo", "程序异常");
                                                            PersonInfoActivity.this.startActivity(intent);
                                                            PersonInfoActivity.this.finish();
                                                        }
                                                    });
                                                }
                                            }
                                        });
                                    } else if (code == 505) {
                                        PersonInfoActivity.this.runOnUiThread((Runnable) new Runnable() {
                                            @Override
                                            public void run() {
                                                final Intent intent = new Intent((Context) PersonInfoActivity.this,  ErrorActivity.class);
                                                intent.putExtra("code", 4);
                                                intent.putExtra("pageStatus", PersonInfoActivity.this.pageStatus);
                                                PersonInfoActivity.this.startActivity(intent);
                                                PersonInfoActivity.this.finish();
                                            }
                                        });
                                    } else {
                                        PersonInfoActivity.this.runOnUiThread((Runnable) new Runnable() {
                                            @Override
                                            public void run() {
                                                PersonInfoActivity.this.mLoading.setVisibility(View.INVISIBLE);
                                                final Intent intent = new Intent((Context) PersonInfoActivity.this,  ErrorActivity.class);
                                                intent.putExtra("code", 1);
                                                intent.putExtra("pageStatus", PersonInfoActivity.this.pageStatus);
                                                intent.putExtra("errorInfo", msg);
                                                PersonInfoActivity.this.startActivity(intent);
                                                PersonInfoActivity.this.finish();
                                            }
                                        });
                                    }
                                }
                            } catch (Exception ex) {
                            }
                        }
                    }).start();
                }
            }
        });
        this.findViewById(R.id.back).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(final View view) {
                final PersonInfoActivity this$0 = PersonInfoActivity.this;
                this$0.startActivity(new Intent((Context) this$0,  MainActivity.class));
                PersonInfoActivity.this.finish();
            }
        });
        this.findViewById(R.id.backTV).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(final View view) {
                final PersonInfoActivity this$0 = PersonInfoActivity.this;
                this$0.startActivity(new Intent((Context) this$0,  MainActivity.class));
                PersonInfoActivity.this.finish();
            }
        });
    }

    @Override
    protected void onCreate(final Bundle bundle) {
        super.onCreate(bundle);
        this.setContentView(R.layout.activity_person_info);
        final Intent intent = this.getIntent();
        this.info = (OrderInfo) intent.getSerializableExtra("info");
        this.pageStatus = intent.getIntExtra("pageStatus", 1);
        this.initView();
    }
}
